﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;

namespace WSDZ.KM.ZH.ExpensesBillSumInvoiceData
{
    [Description("费用报销单保存合计收票信息"), HotUpdate]
    public class SumInvoiceData : AbstractOperationServicePlugIn
    {
        public string logType { get; set; } = "费用报销单保存合计收票信息";
        public override void AfterExecuteOperationTransaction(AfterExecuteOperationTransaction e)
        {
            base.AfterExecuteOperationTransaction(e);
            List<int> IdList = e.DataEntitys.Select(x => x["Id"].ToStringV1().ToInt32V1()).Distinct().Where(y => y != 0).ToList();
            if (IdList == null || IdList.Count < 1)
                return;
            Main(IdList);
        }
        public void Main(List<int> IdList)
        {
            int size = 5000;
            int icount = IdList.Count <= size ? 1 : IdList.Count % size > 0 ? IdList.Count / size + 1 : IdList.Count / size;
            for (int i = 0; i < icount; i++)
            {
                List<int> ids = IdList.Skip(i * size).Take(size).ToList();
                string sql = $@"/*dialect*/ update t_ER_ExpenseReimb set F_RLKX_SPXXJSHJ=ISNULL((select sum(ISNULL(spd.FSUMALLAMOUNT,0)) from T_ER_RECINVENTRY enty
left join T_IV_RECEIVEDINVOICE spd on enty.FRECINV=spd.FID where enty.FID=t_ER_ExpenseReimb.FID),0)
where FID in ({string.Join(",", ids)}) ";
                DBUtils.Execute(Context,sql);
            
            }
      

        }
        
    }

}
